package com.xiaochen.baidu.service.thread;

import com.xiaochen.baidu.service.IMoiuLoginService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import okhttp3.ResponseBody;
import retrofit2.Call;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xc
 * @date 2025/10/12/9:48
 * @desc
 */
@AllArgsConstructor
public class ConfirmThreadTask implements Supplier<String> {

    private String cookie;
    private String clientId;
    private String redirectUri;
    private String state;
    private String responseType;
    private IMoiuLoginService moiuLoginService;
    
    @SneakyThrows
    @Override
    public String get() {
        Call<ResponseBody> confirm = moiuLoginService.confirm(cookie, clientId,redirectUri, state,responseType);
        String result = confirm.execute().headers().get("Location");
        Matcher matcher = Pattern.compile("code=([^&]+)").matcher(result);
        if (matcher.find()) {
            return matcher.group(1);
        }
        return null;
    }
}
